'''
Author: Tyro-yxk tyroyang@foxmail.com
Date: 2024-06-09 14:15:41
LastEditors: Tyro-yxk tyroyang@foxmail.com
LastEditTime: 2024-06-12 22:30:34
FilePath: \python\deldir\logic.py
Description:

Copyright (c) 2024 by Tyro-yxk, All Rights Reserved.
'''

import os
import time

t_now = time.strftime("%Y-%m-%d", time.localtime())
log_path = f"./{t_now}-del.log"
add_path = f"./{t_now}-add.log"


def list_files_recursive(tab_root, tab, path):
    iteam = os.listdir(path)
    tab_root.fast_print(tab, path)
    for i in iteam:
        i_path = os.path.join(path, i).replace("\\", "/")
        if os.path.isdir(i_path):
            try:
                if len(os.listdir(i_path)) == 0:
                    try:
                        os.removedirs(i_path)
                        print("删除目录：" + i_path)
                        tab_root.fast_print(tab, "删除目录：" + i_path)
                        with open(log_path, "a", encoding="utf-8") as f:
                            f.write("删除目录: " + i_path + "\n")
                    except PermissionError:
                        print("没有权限删除目录：", i_path)
                        tab_root.fast_print(tab, "没有权限删除目录：" + i_path)
                else:
                    list_files_recursive(tab_root, tab, i_path)
            except PermissionError:
                print("没有权限访问目录：", i_path)
        else:
            print(i_path)
         


def makdir(tab_root, tab, path):
    with open(path, "r", encoding="utf-8") as file:
        for line in file:
            if not line.strip():
                continue
            dir_path = os.path.join(line.split(":")[1].strip(), line.split(":")[2].strip()).replace("\\", "/")
            print(dir_path)

            try:
                os.makedirs(dir_path)
                tab_root.fast_print(tab, "恢复目录：" + dir_path)
                with open(add_path, "a", encoding="utf-8") as f:
                    f.write("恢复目录: " + dir_path + "\n")
            except FileExistsError:
                print("目录已存在:", dir_path)
                tab_root.fast_print(tab, "目录已存在：" + dir_path)
            except PermissionError:
                print("没有权限创建目录:", dir_path)
                tab_root.fast_print(tab, "没有权限创建目录：" + dir_path)
